package com.ruoyi.kjcg.qygf.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.kjcg.qygf.domain.Qygf;
import com.ruoyi.kjcg.qygf.domain.QygfFj;
import com.ruoyi.kjcg.qygf.service.IQygfService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 企业工法Controller
 *
 * @author huacai
 * @date 2024-11-03
 */
@RestController
@RequestMapping("/kjcg/qygf")
public class QygfController extends BaseController {
    @Resource
    private IQygfService qygfService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询企业工法列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygf:list')")
    @GetMapping("/list")
    public TableDataInfo list(Qygf qygf) {
        startPage();
        List<Qygf> list = qygfService.selectQygfList(qygf);
        return getDataTable(list);
    }

    /**
     * 查询企业工法列表(已发布状态 + 不设数据权限)
     */
    @GetMapping("/getQygfList")
    public TableDataInfo getQygfList(Qygf qygf) {
        startPage();
        List<Qygf> list = qygfService.getQygfList(qygf);
        return getDataTable(list);
    }

    /**
     * 导出企业工法列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygf:export')")
    @Log(title = "企业工法", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Qygf qygf) {
        List<Qygf> list = qygfService.selectQygfList(qygf);
        ExcelUtil<Qygf> util = new ExcelUtil<Qygf>(Qygf.class);
        util.exportExcel(response, list, "企业工法数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Qygf> util = new ExcelUtil<Qygf>(Qygf.class);
        util.importTemplateExcel(response, "企业工法数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "企业工法", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjcg:qygf:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Qygf> util = new ExcelUtil<Qygf>(Qygf.class);
        InputStream inputStream = file.getInputStream();
        List<Qygf> list = util.importExcel(inputStream);
        inputStream.close();
        int count = qygfService.batchInsertQygf(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取企业工法详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygf:query')")
    @GetMapping(value = "/{qygfId}")
    public AjaxResult getInfo(@PathVariable("qygfId") String qygfId) {
        return success(qygfService.selectQygfByQygfId(qygfId));
    }

    /**
     * 新增企业工法
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygf:add')")
    @Log(title = "企业工法", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Qygf qygf) {
        qygf.setUserId(Math.toIntExact(getUserId())); //用户ID
        qygf.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        qygf.setCreateBy(getUsername()); //创建人
        qygf.setIdWhere("存在");
        int i = qygfService.insertQygf(qygf);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(qygf.getQygfId());
        infoRunTaskData.setBusinessType(12); //业务模块: 企业工法-企业工法申报
        infoRunTaskData.setBusinessTitle(qygf.getGfName()); //任务名称: 工法名称
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);
        return toAjax(i);
    }

    /**
     * 修改企业工法
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygf:edit')")
    @Log(title = "企业工法", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Qygf qygf) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(qygf.getGfName()); //任务名称: 工法名称
        infoRunTaskData.setBusinessKey(qygf.getQygfId());
        infoRunTaskData.setBusinessType(12); //业务模块: 企业工法-企业工法申报
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(qygfService.updateQygf(qygf));
    }

    /**
     * 删除企业工法
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygf:remove')")
    @Log(title = "企业工法", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qygfIds}")
    public AjaxResult remove(@PathVariable String[] qygfIds) {
        //同时删除业务关系数据
        for (String qygfId : qygfIds) {
            Qygf qygf = qygfService.selectQygfByQygfId(qygfId);
            iInfoRunTaskDataService.deleteData(qygf.getQygfId(), 12);//业务模块: 企业工法-企业工法申报
        }
        return toAjax(qygfService.deleteQygfByQygfIds(qygfIds));
    }

    /**
     * 获得企业工法主键ID (UUID)
     */
    @GetMapping("/getQyGfId")
    public AjaxResult getQyGfId() {
        String getQyGfId = IdUtils.fastSimpleUUID();
        return success(getQyGfId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertQygfFjByQygfId(@RequestBody QygfFj qygfFj) {
        return toAjax(qygfService.insertQygfFjByQygfId(qygfFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public TableDataInfo selectQygfFjList(QygfFj qygfFj) {
        startPage();
        List<QygfFj> list = qygfService.selectQygfFjList(qygfFj);
        return getDataTable(list);
    }

    /**
     * 删除企业工法附件
     */
    @Log(title = "企业工法附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteQygfFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(qygfService.deleteQygfFjByFjIds(fjIds));
    }

}
